package com.webcash.CNBoard.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Model.WorkSearch;

@Repository
public class WorkRepositoryImpl extends SqlSessionDaoSupport implements WorkRepository {

	public List<Work> selectWorkListByProjectNo(int projectNo, int pageNum) {
		SqlSession session = getSqlSession();
		
		RowBounds bounds = new RowBounds(10 * (pageNum - 1), 10);
		
		
		List<Work> workList = session.selectList(
				"Work.selectWorkListByProjectNoAndPageNum", projectNo, bounds);
		return workList;
	}

	public List<Work> selectNewAllWorksById(String id) {
		SqlSession session = getSqlSession();
		
		List<Work> workList = session.selectList("Work.selectNewAllWorksById", id);
		return workList;
	}

	public List<Work> selectProgressAllWorkListById(String id) {
		SqlSession session = getSqlSession();
		
		List<Work> workList = session.selectList("Work.selectProgressAllWorksById", id);
		return workList;
	}

	public Work selectWorkByWorkNo(int workNo) {
		SqlSession session = getSqlSession();
		
		Work work = session.selectOne("Work.selectWorkByWorkNo",workNo);
		
		return work;
	}

	public void insertIntoWork(Work work) {
		SqlSession session = getSqlSession();
		
		session.insert("Work.insertIntoWork", work);
		
	}

	public List<Work> selectAllWorkListById(String id) {
		SqlSession session = getSqlSession();
		
		return session.selectList("Work.selectAllWorkListById", id);
	}

	public void updateSetWork(Work work) {
		SqlSession session = getSqlSession();
		int workNo = work.getWorkNo();
		Map params = new HashMap();
		params.put("workNo", work.getWorkNo());
		params.put("projectNo", work.getProjectNo());
		params.put("workExplanation", work.getWorkExplanation());
		params.put("workType", work.getWorkType());
		params.put("workState", work.getWorkState());
		params.put("workPriority", work.getWorkPriority());
		params.put("workStartTime", work.getWorkStartTime());
		params.put("workDeadTime", work.getWorkDeadTime());
		params.put("workTitle", work.getWorkTitle());
		params.put("id", work.getId());
		params.put("coWorkers", work.getCoWorkers());
		params.put("workProgress", work.getWorkProgress());
		params.put("upperWorks", work.getUpperWorks());
		
		session.update("Work.updateSetWork", params);
		
	}

	public int selectLastWorkNo() {
		SqlSession session = getSqlSession();
		
		return session.selectOne("Work.selectLastWork");
	}

	public int selectWorkListCountByProjectNo(int projectNo) {
		SqlSession session = getSqlSession();
		
		return session.selectOne("Work.selectWorkListCountByProjectNo", projectNo);
	}

	public List<Work> selectWorkListByProjectNo(int projectNo) {
	SqlSession session = getSqlSession();
		
		List<Work> workList = session.selectList(
				"Work.selectWorkListByProjectNo", projectNo);
		return workList;
	}

	public int selectWorkListCountByProjectNoAndWorkSearch(int projectNo,
			WorkSearch search) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		
		params.put("projectNo", projectNo);
		params.put("workType", search.getWorkType());
		params.put("workState", search.getWorkState());
		params.put("workPriority", search.getWorkPriority());
		params.put("writer", search.getWriter());
		
		int count = session.selectOne("Work.selectWorkListCountByProjectNoAndWorkSearch", params);
		return count;
	}

	public List<Work> selectWorkListByProjectNoAndWorkSearch(int projectNo,
			WorkSearch search, int pageNum) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		
		params.put("projectNo", projectNo);
		params.put("workType", search.getWorkType());
		params.put("workState", search.getWorkState());
		params.put("workPriority", search.getWorkPriority());
		params.put("writer", search.getWriter());
		System.out.println(search.getWriter());
		RowBounds bounds = new RowBounds(10 * (pageNum - 1), 10);
		
		
		return session.selectList("Work.selectWorkListByProjectNoAndWorkSearch", params, bounds);
	}

	public void deleteWorkByWorkNo(int workNo) {
		SqlSession session = getSqlSession();
		
		session.delete("Work.deleteWorkByWorkNo", workNo);
	}

	public List<Work> selectWorkListByProjectNoAndEmpNo(int projectNo, String id) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		params.put("projectNo", projectNo);
		params.put("id", id);
		return session.selectList("Work.selectWorkListByProjectNoAndEmpNo",params);
	}

	
	
	
	
	

}
